package com.xlh.wjdcserver.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.result.Result;
import com.property.dto.*;
import com.property.entity.Role;
import com.property.entity.User;
import com.property.vo.RoleVO;
import com.property.vo.UserLoginVO;
import com.property.vo.UserVO;
import com.xlh.wjdcserver.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
@Tag(name = "用户接口")
public class UserController {

    @Autowired
    private UserService userService;

    @Operation(description = "用户登录")
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        return Result.ok(userService.login(userLoginDTO));
    }

    @Operation(description = "用户填写问卷登录")
    @PostMapping("/qlogin")
    public Result<UserLoginVO> qlogin(@RequestBody UserQLoginDTO userQLoginDTO) {
        return Result.ok(userService.qlogin(userQLoginDTO));
    }

    @Operation(description = "个人中心")
    @GetMapping("/center/{userId}")
    public Result<UserVO> center(@PathVariable String userId)
    {
        return Result.ok(userService.center(userId));
    }

    @Operation(description = "修改用户信息")
    @PutMapping("/updateUser")
    public Result updateUser(@RequestBody UserDTO userDTO)
    {
        userService.updateUser(userDTO);
        return Result.ok();
    }

    @Operation(description = "修改密码")
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody UserPwdDTO userPwdDTO)
    {
        userService.updatePassword(userPwdDTO);
        return Result.ok();
    }

    @Operation(description = "用户退出登录")
    @PostMapping("/logout")
    public Result logout()
    {
        userService.logout();
        return Result.ok();
    }

    @Operation(description = "上传头像")
    @PostMapping("/uploadAvatar")
    public Result uploadAvatar(@RequestParam("file") MultipartFile file)
    {
        return Result.ok(userService.uploadAvatar(file));
    }

    @Operation(description = "获取用户分页")
    @GetMapping("/getUserPage")
    public Result<Page<UserVO>> getUserPage(UserPageDTO userPageDTO)
    {
        return Result.ok(userService.getUserPage(userPageDTO));
    }

    @Operation(description = "给用户分配角色")
    @PostMapping("/assignRole")
    public Result assignRole(@RequestBody AssignRoleDTO assignRoleDTO)
    {
        userService.assignRole(assignRoleDTO);
        return Result.ok();
    }

    @Operation(summary = "根据用户id获取角色")
    @GetMapping("/getRoleByUserId")
    public Result<List<RoleVO>> getRoleByUserId(@RequestParam String userId)
    {
        return Result.ok(userService.getRoleByUserId(userId));
    }
}
